package org.budo.boot.starter.graph.spring.boot.autoconfigure;

import org.budo.graph.config.BudoGraphConfigurer;
import org.budo.graph.view.spring.boot.BudoGraphViewFilterRegistrationBean;
import org.budo.support.lang.util.NumberUtil;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.env.Environment;
import org.springframework.util.Assert;

import lombok.extern.slf4j.Slf4j;

/**
 * @author limingwei
 * @see com.alibaba.druid.spring.boot.autoconfigure.DruidDataSourceAutoConfigure
 */
@Configuration
@Slf4j
public class BudoBootStarterGraphSpringBootAutoconfigure {
    @Bean
    public BudoGraphConfigurer budoGraphConfigurer(Environment environment) {
        String redisHost = environment.getProperty("budo.graph.redis.host");
        Assert.hasText(redisHost, "need budo.graph.redis.host in your application conf");

        String redisPort = environment.getProperty("budo.graph.redis.port", "6379");
        String redisPassword = environment.getProperty("budo.graph.redis.password");

        String aopPatterns = environment.getProperty("budo.graph.aop.patterns");
        String dubboRegistryAddress = environment.getProperty("spring.dubbo.registry.address");
        String dubboApplicationName = environment.getProperty("spring.dubbo.application.name", BudoGraphConfigurer.dubboApplicationName());

        BudoGraphConfigurer budoGraphConfigurer = new BudoGraphConfigurer();
        budoGraphConfigurer.setAopPatterns(aopPatterns);
        budoGraphConfigurer.setRedisHost(redisHost);
        budoGraphConfigurer.setRedisPort(NumberUtil.toInteger(redisPort));
        budoGraphConfigurer.setRedisPassword(redisPassword);
        budoGraphConfigurer.setDubboRegistryAddress(dubboRegistryAddress);
        budoGraphConfigurer.setDubboApplicationName(dubboApplicationName);

        log.info("#17 budoGraphConfigurer=" + budoGraphConfigurer);
        return budoGraphConfigurer;
    }

    @Bean
    public FilterRegistrationBean budoGraphViewFilterRegistrationBean() {
        return new BudoGraphViewFilterRegistrationBean();
    }
}